package com.baizhi.cmfz.controller;

import com.baizhi.cmfz.annottation.AddCacheAnnotation;
import com.baizhi.cmfz.annottation.DeleteCacheAnnotation;
import com.baizhi.cmfz.annottation.LogAnnottion;
import com.baizhi.cmfz.entity.Guru;
import com.baizhi.cmfz.service.GuruService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.*;

@Controller
@RequestMapping("/guru")
public class GuruController {
    @Autowired
    private GuruService guruService;

    @RequestMapping("/selectByPageGuru")
    @ResponseBody
    @LogAnnottion(type = "select", value = "查询所有上师")
    @AddCacheAnnotation
    public Map selectByPageGuru(Integer page, Integer limit, String name) {
        return guruService.selectByPageGuru(page, limit, name);
    }

    @RequestMapping("/insertGuru")
    @ResponseBody
    @LogAnnottion(type = "insert", value = "添加上师")
    @DeleteCacheAnnotation
    public Map insertGuru(Guru guru, MultipartFile file, HttpServletRequest request) throws IOException {
        //获取文件夹在服务器的绝对路径
        String realPath = request.getServletContext().getRealPath("/img");
        //文件的名字
        String originalFilename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        String newFileName = uuid + "_" + originalFilename;
        //通过输出流将文件写入到服务器磁盘
        file.transferTo(new File(realPath + "/" + newFileName));
        System.out.println("上师=========122232=========");
        guru.setGuruImage(newFileName);
        return guruService.insertGuru(guru);
    }

    @ResponseBody
    @RequestMapping("/updateStatus")
    @LogAnnottion(type = "update", value = "修改上师")
    @DeleteCacheAnnotation
    public Map updateStatus(Integer id, Integer state) {
        return guruService.updateState(id, state);
    }

    @RequestMapping("/selectOneGuru")
    @ResponseBody
    @LogAnnottion(type = "select", value = "查询一个上师")
    @AddCacheAnnotation
    public Guru selectOneGuru(Integer id) {

        return guruService.selectOneGuru(id);

    }

    @ResponseBody
    @RequestMapping("/updateGuru")
    @LogAnnottion(type = "update", value = "修改上师")
    @DeleteCacheAnnotation
    public Map updateGuru(Guru guru, MultipartFile file, HttpServletRequest request) throws IOException {

        if (file != null) {
            String realPath = request.getServletContext().getRealPath("/img");
            System.out.println(file.getOriginalFilename() + "替换--------");
            String uuid = UUID.randomUUID().toString();
            //新的文件名字
            String newFileName = uuid + "_" + file.getOriginalFilename();
            //写入服务器磁盘
            file.transferTo(new File(realPath + "/" + newFileName));
            guru.setGuruImage(newFileName);
        }
        System.out.println(guru + "===========================");
        return guruService.updateGuru(guru);
    }

    @RequestMapping("/deleteGuru")
    @ResponseBody
    @LogAnnottion(type = "delete", value = "删除上师")
    @DeleteCacheAnnotation
    public Map deleteGuru(Integer id) {

        return guruService.deleteGuru(id);
    }

    @RequestMapping("/upload")
    @ResponseBody
    //批量添加
    @LogAnnottion(type = "insertCount", value = "批量添加上师")
    @DeleteCacheAnnotation
    public Map upload(MultipartFile file) throws IOException {


        Map map = guruService.insertCountGuru(file.getInputStream());


        return map;
    }
    @RequestMapping("/downloadGuru")
    @LogAnnottion(type = "download", value = "下载上师数据")
    public  void downloadGuru(int [] ids , HttpServletResponse response) throws IOException, IllegalAccessException {
        System.out.println(Arrays.toString(ids));
        response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode("excel.xls","UTF-8"));
        response.setContentType("application/vnd.ms-excel");
        HSSFWorkbook workbook = guruService.downloadCountGuru(ids);
        System.out.println(workbook);
        //响应给客户端
        workbook.write(response.getOutputStream());

    }
}